/* eslint-disable */
/**
 * 文件hash效验
 * @description api/chkfile/{fkey}
 * @param {*} url
 * @param {*} fileObject
 * @param {*} headers
 * @param {*} onload
 * @param {*} onerror
 */
function checkFile(url, fileObject, headers, onload, onerror) {
  var xhr = new XMLHttpRequest(),
    fkey = fileObject.fkey;
  url += "/api/chkfile/" + fkey;
  xhr.open("get", url, true);
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  xhr.onload = (e) => {
    onload(xhr.response);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  xhr.send();
}

/**
 * 块hash效验
 * @description api/chkchunk/{fkey}/index/{index}/hash/{hash}
 * @param {*} url
 * @param {*} chunkObject
 * @param {*} headers
 * @param {*} onload
 * @param {*} onerror
 */
function checkChunk(url, chunkObject, headers, onload, onerror) {
  var xhr = new XMLHttpRequest(),
    fkey = chunkObject.fkey,
    index = chunkObject.index,
    hash = chunkObject.hash;
  url += "/api/chkchunk/" + fkey + "/index/" + index + "/hash/" + hash;
  xhr.open("get", url, true);
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  xhr.onload = (e) => {
    onload(xhr.response);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  try {
    xhr.send();
  } catch (e) {}
}

function makeFile(url, fileObject, headers, onload, onerror) {
  let xhr = new XMLHttpRequest(),
    fkey = fileObject.fkey,
    form = new FormData();
  //console.log(form.get("file"));
  url += "/api/mkfile/" + fkey;
  xhr.open("post", url); //if ArrayBuffer,here must be false
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  //xhr.responseType = "json";
  xhr.timeout = 3000;
  xhr.onload = (e) => {
    onload(e);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  try {
    xhr.send(form);
  } catch (e) {
    console.error(e);
  }
}
/**
 * ArrayBuffer上传
 * @description api/mkchunk/{fkey}/index/{index}/hash/{hash}
 * @param {*} url
 * @param {*} buffer
 * @param {*} headers
 * @param {*} onload
 * @param {*} onprogress
 * @param {*} onerror
 */
function uploadStream(
  url,
  buffer,
  chunkObject,
  headers,
  onload,
  onprogress,
  onerror
) {
  let xhr = new XMLHttpRequest(),
    fkey = chunkObject.fkey,
    index = chunkObject.index,
    hash = chunkObject.hash,
    form = new FormData();
  form.append("file", new File([buffer], index));
  //console.log(form.get("file"));
  url += "/api/putchunk/" + fkey + "/index/" + index + "/hash/" + hash;
  //https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/open
  xhr.open("post", url); //if ArrayBuffer,here must be false
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  //xhr.responseType = "json";
  xhr.timeout = 30000;
  xhr.onload = (e) => {
    onload(e);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  xhr.upload.onprogress = (e) => {
    onprogress(e);
  };
  try {
    xhr.send(form);
  } catch (e) {
    console.error(e);
  }
}

/**
 * 文件上传
 * @description api/upload/file/{fkey}
 * @param {*} url
 * @param {*} fileObject
 * @param {*} headers
 * @param {*} onload
 * @param {*} onprogress
 * @param {*} onerror
 */
function uploadFile(url, fileObject, headers, onload, onprogress, onerror) {
  var form = new FormData(),
    fkey = fileObject.name;
  form.append("file", fileObject.file);
  url += "/api/upload/file/" + fkey;
  var xhr = new XMLHttpRequest();
  xhr.open("post", url, true); //if multipart,here is must be true
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  xhr.onload = (e) => {
    onload(xhr.response);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  xhr.upload.onprogress = (e) => {
    onprogress(e);
  };
  //console.log(url);
  try {
    xhr.send(form);
  } catch (e) {
    console.error(e);
  }
}

/**
 * 文件直接下载
 * @description api/download/file/{fkey}
 * @param {*} url
 * @param {*} fkey
 */
function downloadFile(url, fkey, onload, onerror) {
  var xhr = new XMLHttpRequest();
  url += "/api/download/file/" + fkey;
  xhr.open("get", url, true);
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  xhr.onload = onload;
  xhr.onerror = onerror;
  xhr.send();
}

/**
 * 文件片信息
 * @description api/download/chunks-indexes/{fkey}
 * @param {*} url
 * @param {*} fkey
 */
function getAllChunks(url, fkey, onload, onerror) {
  var xhr = new XMLHttpRequest();
  url += "/api/download/chunks-indexes/" + fkey;
  xhr.open("get", url, true);
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  xhr.onload = (e) => {
    onload(xhr.response);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  xhr.send();
}

/**
 * 片下载
 * @description api/download/chunks/{fkey}/{index}
 * @param {*} url
 * @param {*} fkey
 * @param {*} index
 */
function downloadBuffer(url, fkey, index) {
  var xhr = new XMLHttpRequest();
  url += "/api/download/chunks/" + fkey + "/" + index;
  xhr.open("get", url, true);
  for (let key in headers) {
    xhr.setRequestHeader(key, headers[key]);
  }
  xhr.responseType = "arraybuffer";
  xhr.onload = (e) => {
    onload(xhr.response);
  };
  xhr.onerror = (e) => {
    onerror(e);
  };
  xhr.send();
}
